public class SeqString implements IString {
    private char[] mainString; // 字符数组存放主串值
    private int lenght;        // 当前主串长度

    public SeqString() {
        this(0);
    }

    public SeqString(int len) {
        mainString = new char[len];
        lenght = len;
    }

    public SeqString(String str) {
        mainString = str.toCharArray();
        lenght = mainString.length;
    }

    public SeqString(char[] value) {
        mainString = new char[value.length];
        for (int i = 0; i < value.length; i++) {
            mainString[i] = value[i];
        }
        lenght = value.length;
    }

    public void clear() { // 将一个已经存在的字符串置成空串
        lenght = 0;
    }

    public boolean isEmpty() { // 判断字符串是否为空
        return lenght == 0;
    }

    public int length() { // 返回字符串长度
        return lenght;
    }

    /**
     * 返回字符串中序号为index的字符
     */
    public char charAt(int index) {
        if (index < 0 || index >= lenght) {
            throw new StringIndexOutOfBoundsException(index);
        }
        return mainString[index];
    }

    /**
     * 扩充字符串存储空间容量，参数指定容量
     */
    public void allocate(int newcapacity) {
        char[] temp = mainString;
        mainString = new char[newcapacity];
        for (int i = 0; i < temp.length; i++)
            mainString[i] = temp[i];
    }

    /**
     * 截取从begin到end-1的子串
     * @param end 逻辑位置
     *
     */
    public String subString(int begin, int end) {
        if (begin < 0)
            throw new StringIndexOutOfBoundsException("起始位置不能小于0");
        if (end > lenght)
            throw new StringIndexOutOfBoundsException("结束位置不能大于串的当前长度");
        if (begin > end)
            throw new StringIndexOutOfBoundsException("开始位置不能大于结束位置");
        if (begin == 0 && end == lenght)
            return null;
        char[] buffer = new char[end - begin];
        for (int i = 0; i < buffer.length; i++) {
            buffer[i] = this.mainString[i + begin];
        }
        return String.valueOf(buffer);
    }

    /**
     * 在第offset字符之前插入str
     */
    public void insert(int pos, IString subString) {
        if (pos <= 0 || pos > this.lenght + 1) {
            throw new StringIndexOutOfBoundsException("" +
                    "插入位置不合法(有效位置从1开始)!");
        }
        int subLen = subString.length();
        int newcount = subLen + lenght;

        // 若新串长度大于原主串长度,则扩容
        if (newcount > mainString.length) {
            allocate(newcount);
        }

        // 主串给子串 '腾位置'
        for (int i = 0; i < lenght - pos + 1; i++) {
            mainString[i + subLen + pos - 1] = mainString[i + pos - 1];
        }

        // 子串插入主串
        for (int i = 0; i < subLen; i++) {
            mainString[i + pos - 1] = subString.charAt(i);
        }

        lenght = newcount;
        this.toString();
    }

    /**
     * 删除从begin到end之前(end-1)的子串
     */
    public void delete(int begin, int end) {
        for (int i = 0; i < lenght - end + 1; i++) {
            mainString[i + begin - 1] = mainString[i + end - 1];
        }
        lenght = lenght - (end - begin);
        this.toString();
    }

    public int compareTo(IString str) { // 将当前串与str比较
        int len1 = lenght;
        int len2 = str.length();
        int n = Math.min(len1, len2);
        char[] s1 = mainString;
        char[] s2 = new char[str.length()];
        for (int i = 0; i < str.length(); i++)
            s2[i] = str.charAt(i);
        int k = 0;
        while (k < n) {
            char ch1 = s1[k];
            char ch2 = s2[k];
            if (ch1 != ch2) {
                return ch1 - ch2;
            }
            k++;
        }
        return len1 - len2;
    }

    public void concat(IString str) {
        insert(lenght, str);
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        if (lenght == 0) {
            System.out.print("空串!");
            System.out.print("\n");
            return "";
        }
        for (int i = 0; i < this.lenght; i++) {
            System.out.print(this.mainString[i]);
            builder.append(this.mainString[i]);
        }
        System.out.print("\n");
        return builder.toString();
    }


}
